package com.flyme.module.base.provider.service.impl;

import com.flyme.common.core.model.ResultBody;
import com.flyme.common.core.model.query.CriteriaQuery;
import com.flyme.common.core.model.query.PagerInfo;
import com.flyme.common.core.model.query.PqGrid;
import com.flyme.common.core.utils.ObjectUtils;
import com.flyme.core.base.supper.entity.EntityMap;
import com.flyme.core.base.supper.service.impl.BaseServiceImpl;
import com.flyme.module.base.client.entity.SysBalance;
import com.flyme.module.base.client.entity.SysBalancelog;
import com.flyme.module.base.provider.mapper.SysBalancelogMapper;
import com.flyme.module.base.provider.service.SysBalancelogService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * @author: zyf
 * @date: 2018/10/24 16:33
 * @description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SysBalancelogServiceImpl extends BaseServiceImpl<SysBalancelogMapper, SysBalancelog> implements SysBalancelogService {

    @Override
    public ResultBody insert(SysBalance balance, String outTradeNo, BigDecimal amount, Integer type, String title, String body) {
        if (ObjectUtils.isNotEmpty(balance)) {
            SysBalancelog balanceLog = new SysBalancelog();
            outTradeNo = ObjectUtils.isNotEmpty(outTradeNo) ? outTradeNo : String.valueOf(balanceLog.getId());
            balanceLog.setBalanceId(balance.getId());
            balanceLog.setTradeNo(outTradeNo);
            balanceLog.setAccountInfoId(balance.getAccountInfoId());
            balanceLog.setTitle(title);
            balanceLog.setBody(body);
            balanceLog.setAmount(amount);
            balanceLog.setType(type); // 类型 1.充值2.消费 3.提现 4.分销奖励,5:退款
            save(balanceLog);
        } else {
            return ResultBody.failed("账户信息不存在!");
        }
        return ResultBody.ok("账户变更记录添加成功");
    }

    @Override
    public PqGrid getBalanceLogList(CriteriaQuery criteriaQuery) {
        PqGrid pqGrid = pageList(criteriaQuery);
        return pqGrid;
    }

    /**
     * 根据用户查询学习币记录
     */
    public PqGrid selectByUser(PagerInfo pagerInfo, Long userId){
        CriteriaQuery queryWrapper = new CriteriaQuery<EntityMap>(pagerInfo);
        queryWrapper.select("*");
        queryWrapper.eq("accountInfoId", userId);
        queryWrapper.eq("type", 1);
        queryWrapper.orderByDesc("createTime");
        return pageList(queryWrapper);
    }

}
